<?php
namespace App\Http\Services;

use App\Http\Model\FinanceRechargeOrder;
use App\Http\Model\Gift;
use App\Http\Model\Hint;
use App\Http\Model\TogetherGame;
use App\Http\Model\User;
use Illuminate\Support\Facades\Redis;

class TogetherGameService extends CommonServices
{
    protected $togetherGameModel;//一起玩表
    protected $financeRechargeOrderModel;//充值订单表
    protected $userModel;//用户
    protected $gift_model;//礼物

    public function __construct()
    {
        $this->togetherGameModel = new TogetherGame();
        $this->financeRechargeOrderModel = new FinanceRechargeOrder();
        $this->userModel = new User();
        $this->gift_model = new Gift();
    }

    //获取总开关控制
    public function togetherGamePermission($userId){
        if(empty($userId)){
            return 0;
        }
        do{
            $togetherGameOpen = 0;
            //查看后台配置开关权限
            $gameOpenConfig = $this->getTogetherGameConfig();
            if(empty($gameOpenConfig)){
                $togetherGameOpen = 0;
                break;
            }
            //查看用户充值钻石总金额是否大于配置的金额
            $userRecharge = $this->getUserRecharge($userId);
            if($userRecharge >= $gameOpenConfig['diamond']){
                $togetherGameOpen = 1;
                break;
            }
        }while(false);
        //白名单用户
        $test_user_ids = $this->userModel->get_test_users();
        if(in_array($userId,$test_user_ids)){
            $togetherGameOpen = 1;
        }
        return $togetherGameOpen;
    }

    //数据处理
    public function dataChange($id, $gameName, $diamond_value, $jump_url, $video_url, $scale, $sort, $status, $defaultUrl, $iconUrl){
        $params = [
            'id' => $id,//数据id
            'game_name' => $gameName,//游戏名称
            'diamond_value' => $diamond_value,//钻石限制
            'jump_url' => $jump_url,//跳转链接
            'video_url' => $video_url,//视频链接
            'default_url' => $defaultUrl,
            'icon_url' => $iconUrl,
            'scale' => $scale,//视频比例
            'sort' => $sort,//排序更改
            'status' => $status,//显示状态
        ];
        //处理
        $status = $this->togetherGameModel->dataChange($params);
        return $status;
    }

    //删除数据
    public function deleteGame($id){
        $status = $this->togetherGameModel->deleteGame($id);
        return $status;
    }

    //总开关设置
    public function setGameOpen($status,$diamond_value){
        $status = $this->togetherGameModel->setGameOpen($status, $diamond_value);
        return $status;
    }

    //一起玩签到配置
    public function setDaySign($id, $signday, $img, $sendType, $sendId, $sendNum, $status){
        $params = [
            'id' => $id,//数据id
            'sigin_day' => $signday,//签到天
            'pic' => $img,//签到链接
            'send_type' => $sendType,//发放类型
            'send_num' => $sendNum,//发送数量
            'send_id' => $sendId,//发送奖励id
            'status' => $status,//显示状态
        ];
        $data_status = $this->togetherGameModel->setDaySign($params);
        return $data_status;
    }

    //客户端-查看一起玩后台开关配置接口
    public function getTogetherGameConfig(){
        $list = $this->togetherGameModel->getTogetherGameConfig();
        return $list;
    }

    //客户端-获取游戏列表
    public function getGameList($user_id){
        //查询用户充值钻石
        $diamond = $this->getUserRecharge($user_id);
        $list = $this->togetherGameModel->getGameList();
        if(!empty($list)){
            foreach($list as $k=>&$v){
                //是否满足钻石数
                if($diamond < $v['diamond_value']){
                    unset($list[$k]);
                    continue;
                }
                $v['video_url'] = build_url($v['video_url']);
                //获取各个游戏昨天人数
                $gamePeople = $this->togetherGameModel->getYestorDayCount();
                if(!isset($gamePeople[$v['game_name']])){
                    $v['join_data'] = [
                        'nums' => 0,
                        'peopleImg' => [],
                    ];
                }else{
                    $v['join_data'] = json_decode($gamePeople[$v['game_name']],true);
                }
                $scale = explode(':',$v['scale']);
                $v['scale_W'] = isset($scale[0]) ? $scale[0] : 1;
                $v['scale_H'] = isset($scale[1]) ? $scale[1] : 1;
                $v['default_video_img'] = build_image($v['default_url']);
                $v['icon_img'] = build_image($v['icon_url']);
            }
        }
        return array_values($list);
    }

    //客户端-获取签到列表
    public function getGameSignList($user_id){
        $signList = $this->togetherGameModel->getSignList();
        //获取是否签到过(若断签则重新签到)
        $getSignTimes = $this->togetherGameModel->getSignTimes($user_id);
        if(!empty($signList)){
            //获取图片地址
            foreach($signList as &$v){
                $v['is_sign'] = 0;
                $v['pic'] = build_image($v['pic']);//图片
                $v['sigin_day'] = '第'.numberToChinese($v['sigin_day']).'天';
                if($getSignTimes == 0){
                    $v['is_sign'] = 0;
                }else{
                    //判断此数据是否签到
                    $checkIssetSign = $this->togetherGameModel->checkIssetSign($user_id,$v['id']);
                    if($checkIssetSign && $getSignTimes != 0){
                        $v['is_sign'] = 1;
                    }
                }
            }
        }
        $todaySign = $this->getTodaySign($user_id);
        $data = [
            'today_sign' => $todaySign,
            'signList' => $signList
        ];
        return $data;
    }
    
    //客户端-查看用户是否签到
    public function getTodaySign($user_id){
        $checkToday = $this->togetherGameModel->checkTodaySign($user_id);
        if($checkToday){
            return 1;
        }
        return 0;
    }

    //客户端-用户签到
    public function dailyGameSetSign($user_id,$signId){
        do{
            $code = Hint::SUCCESS;
            $msg = '签到成功';
            //查看当日是否签到过
            $checkToday = $this->togetherGameModel->checkTodaySign($user_id);
            if($checkToday){
                $code = Hint::ERR_NORMAL;
                $msg = '今日已签到';
                break;
            }
            //进行签到处理
            $setSign = $this->togetherGameModel->setTodaySign($user_id,$signId);
            if(empty($setSign)){
                $code = Hint::ERR_NORMAL;
                $msg = '请重新尝试~';
                break;
            }
            $msg = $setSign;
        }while(false);
        return compact('code','msg');
    }

    //获取用户充值总金额
    public function getUserRecharge($userId){
        $userRecharge = $this->financeRechargeOrderModel->getRechargeTotal($userId);
        return $userRecharge/10;
    }

    //获取游戏滚动公告
    public function getGameNotice(){
        $list = $this->togetherGameModel->getGameNotice();
        if(empty($list)){
            return [];
        }
        $newData = [];
        //获取活动配置
        $gameConfig = $this->togetherGameModel->getGameNameList();
        $gameConfig = array_column($gameConfig,null,'game_type');
        //数据整理
        foreach($list as $k=>$v){
            $newData[$k]['game_type'] = $v['game_type'];
            $newData[$k]['game_name'] = isset($gameConfig[$v['game_type']]) ? $gameConfig[$v['game_type']]['game_name'] : '';
            $newData[$k]['jump_url'] = isset($gameConfig[$v['game_type']]) ? $gameConfig[$v['game_type']]['jump_url'] : '';
            if($v['game_type'] == 5){
                //许愿池特殊处理
                $luckData = json_decode($v['content'],true);
                $userInfo = $this->userModel->get_user($v['user_id'],false);
                $giftInfo = $this->gift_model->get_gift($luckData[0]['id']);
                $msg = [
                    'now' => time(),
                    'type' => $luckData[0]['type'],
                    'gameId' => $luckData[0]['type'],
                    'itemName' => '',
                    'userName' => $userInfo['nickname'],
                    'giftName' => $giftInfo['name'],
                ];
                $content = $msg;
            }elseif($v['game_type'] == 3){
                //开运装备星
                $content = json_decode($v['content'],true);
                $content['giftName'] = $content['equipName'];
                unset($content['equipName']);
            }else{
                //西天取经
                $notice = json_decode($v['content'],true);
                if(isset($notice['giftList'])){
                    $giftName = array_column($notice['giftList'],'gift_name');
                    $notice['giftName'] = empty($giftName) ? $notice['giftList']['gift_name'] : implode(',',$giftName);
                }
                $content = $notice;
            }
            $newData[$k]['content'] = $content;
        }
        return $newData;
    }
}